﻿using System;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Category;

namespace KMT.Controls.Product
{
    public partial class ListCategoryByParent : System.Web.UI.UserControl
    {
        public string IsShow = "none";

        protected int CategoryId;

        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryId          = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
            ltCategoryName.Text = CategoryController.GetCategoryName(CategoryId);

            if (!IsPostBack)
                GetListCategoryByParent();
        }

        private void GetListCategoryByParent()
        {
            var lstCategory = CategoryController.Category_GetByParentId((int)CategoryType.Product, CategoryId, Constants.ACTIVE);
            if (lstCategory.Count > 0)
            {
                rptCategory.DataSource = lstCategory;
                rptCategory.DataBind();

                IsShow = "block";
            }
        }

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var ltCategory      = (Literal)e.Item.FindControl("ltCategory");
                var categoryId      = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
                var categoryName    = ((HiddenField)e.Item.FindControl("hfCategoryName")).Value;
                var unsignCategory  = ((HiddenField)e.Item.FindControl("hfUrl")).Value;

                ltCategory.Text = string.Format("<a href=\"{0}\">{1}</a>", UrlHelpers.ListProductByCategory(categoryId, unsignCategory), categoryName);
            }            
        }
    }
}